summaryrefslogtreecommitdiffstats
path: root/glucometerutils/support/hiddevice.py
diff options
context:
space:
mode:
Diffstat (limited to 'glucometerutils/support/hiddevice.py')
-rw-r--r--glucometerutils/support/hiddevice.py58
1 files changed, 25 insertions, 33 deletions
diff --git a/glucometerutils/support/hiddevice.py b/glucometerutils/support/hiddevice.py
index 43461e7..41ad17c 100644
--- a/glucometerutils/support/hiddevice.py
+++ b/glucometerutils/support/hiddevice.py
@@ -6,44 +6,34 @@
import logging
import os
-from typing import BinaryIO, Optional, Text
+from typing import BinaryIO, Optional, Text, Tuple
from glucometerutils import exceptions
-class HidDevice:
- """A device speaking USB HID protocol driver base.
+class HidSession:
+ """An access class to speak to USB HID based devices.
- This class does not implement an actual driver by itself, but provides an
- easier access to the boilerplate code required for speaking USB HID.
-
- This helper wraps around an optional dependency on hidapi library: if
- present the driver will auto-detect the device, if not the device path needs
- to be provided and should point to a device implementing Linux's hidraw
- interface.
-
- The following constants can be set by the actual drivers:
-
- USB_VENDOR_ID: (int) USB vendor ID for the device.
- USB_PRODUCT_ID: (int) USB product ID for the device.
-
- If the VID/PID pair is not provided, the driver will require a device path
- to be used.
-
- Optional parameters available:
-
- TIMEOUT_MS: (int, default: 0) the read timeout in milliseconds, used
- for hidapi reads only. If < 1, hidapi will be provided no timeout.
+ This class does not implement a full driver, but rather provide simpler read/write
+ methods abstracting the HID library.
"""
- USB_VENDOR_ID = None # type: int
- USB_PRODUCT_ID = None # type: int
+ def __init__(self, usb_id, device, timeout_ms=0):
+ # type: (Optional[Tuple[int, int]], Optional[Text], int) -> None
+ """Construct a new session object.
+
+ Args:
+ usb_id: Optional pair of vendor_id and product_id for the session.
+ This is required to use the hidapi library.
+ device: Optional path to Linux hidraw-style device path. If not provided,
+ usb_id needs to be provided instead.
+ timeout_ms: Timeout in milliseconds for read operations. Only relevant when
+ using hidapi library.
+ """
- TIMEOUT_MS = 0 # type: int
+ self._timeout_ms = timeout_ms
- def __init__(self, device):
- # type: (Optional[Text]) -> None
- if None in (self.USB_VENDOR_ID, self.USB_PRODUCT_ID) and not device:
+ if not usb_id and not device:
raise exceptions.CommandLineError(
"--device parameter is required, should point to a /dev/hidraw "
"device node representing the meter."
@@ -63,8 +53,10 @@ class HidDevice:
try:
import hid
+ assert usb_id
+ vendor_id, product_id = usb_id
self.hidapi_handle_ = hid.device()
- self.hidapi_handle_.open(self.USB_VENDOR_ID, self.USB_PRODUCT_ID)
+ self.hidapi_handle_.open(vendor_id, product_id)
except ImportError:
raise exceptions.ConnectionFailed(
message='Missing requied "hidapi" module.'
@@ -74,7 +66,7 @@ class HidDevice:
message=f"Unable to connect to meter: {e}."
)
- def _write(self, report):
+ def write(self, report):
# type: (bytes) -> None
"""Writes a report to the HID handle."""
@@ -86,7 +78,7 @@ class HidDevice:
if written < 0:
raise exceptions.CommandError()
- def _read(self, size=64):
+ def read(self, size=64):
# type: (int) -> bytes
"""Read a report from the HID handle.
@@ -96,4 +88,4 @@ class HidDevice:
if self.handle_:
return bytes(self.handle_.read(size))
- return bytes(self.hidapi_handle_.read(size, timeout_ms=self.TIMEOUT_MS))
+ return bytes(self.hidapi_handle_.read(size, timeout_ms=self._timeout_ms))